<?php
abstract class LetsXmlDataImporter
{
  public static function importMembers(sfAction $action,$file,$asso_id)
  {
    if(file_exists($file))
    {
      $xml = simplexml_load_file($file);
      $STATUS_ID = 5;
      $STATE_ID = MemberTable::STATE_ENABLED;
      $CUSTO_FIELD_PERMANENT =3;
      $CUSTO_FIELD_DETAIL =1;
      $CUSTO_FIELD_DOCUMENT =2;
      $GROUP_MEMBER_ID =2;
      
      $MAPPING_CIVILITE = array( '1'=> 'Mr', '2'=>'Mme','3'=>'Melle');
      $MAPPING_DOCUMENT = array( '0'=> 'Non choisi', '1'=> 'Papier','2' => 'Papier et Email','3'=>'Email');
      $startIndex = 700;
      $endIndex=1200;
      $index=-1;
      $nbImported =0;
      foreach($xml as $xml_member)
      {
        $index++;
        if($index < $startIndex)
        {
          continue;
        }
        else if($index >= $endIndex)
        {
          break;
        }
        $statut = (string)$xml_member->activite_adh;
        $new_member = new Member();
        // membre actif
        if($statut == '1')
        {
          
          $new_member->setUsername((string)$xml_member->id_adh);
          $new_member->setState($STATE_ID);
          $new_member->setStatusId($STATUS_ID);
          $new_member->setFirstname((string)$xml_member->nom_adh);
          $new_member->setLastname((string)$xml_member->prenom_adh);
          $new_member->setCivility($MAPPING_CIVILITE[((string)$xml_member->titre_adh)]);
          $new_member->setStreet(stripslashes((string)$xml_member->adresse_adh));
          $new_member->setZipcode((string)$xml_member->cp_adh);
          // remplacer \' par '
          $city = (string)$xml_member->ville_adh;
          
          $id_adh = intval((string)$xml_member->id_adh);
          
          $new_member->getUserGuard()->setId($id_adh);
          $new_member->setId($id_adh);
          
          $new_member->setCity(stripslashes($city));
          if($xml_member->tel_adh)
          {
            $new_member->setPhoneHome((string)$xml_member->tel_adh);
          }
          if($xml_member->gsm_adh)
          {
            $new_member->setPhoneMobile((string)$xml_member->gsm_adh);
          }
          if($xml_member->email_adh)
          {
            $new_member->setEmail((string)$xml_member->email_adh);
          }
          $distrib_doc = $MAPPING_DOCUMENT[(string)$xml_member->envoiDocs_adh];
          $detail_member =  (string)$xml_member->info_adh;
          
          $new_member->setPassword((string)$xml_member->mdp_adh);
          $new_member->setSubscriptionDate((string)$xml_member->date_crea_adh);
          $geoloc_err = (string)$xml_member->geoloc_error_adh;
          if($geoloc_err == '0')
          {
            $new_member->setLongitude(doubleval((string)$xml_member->lng_adh));
            $new_member->setLatitude(doubleval((string)$xml_member->lat_adh));
          }
          $new_member->setAssociationId($asso_id);
          $new_member->save();
          
          $event = new sfEvent($action, 'member.created',array(
            'member' => $new_member,
            'first' => false,
          ));
          LetsEventsListener::memberCreatedEvent($event);
          $member_id = $new_member->getId();
          // infos complementaire
          $extra = new MemberExtraValue();
          $extra->setValue($detail_member);
          $extra->setMemberId($member_id);
          $extra->setRowId($CUSTO_FIELD_DETAIL);
          $extra->save();
          // ditrib docs
          $extra = new MemberExtraValue();
          $extra->setValue($distrib_doc);
          $extra->setMemberId($member_id);
          $extra->setRowId($CUSTO_FIELD_DOCUMENT);
          $extra->save();
          // group de droit
          $group = new sfGuardUserGroup();
          $group->setGroupId($GROUP_MEMBER_ID);
          $group->setUserId($member_id);
          $group->save();
          
          $nbImported++;
        }
        
        
      }
    }
    else{
      throw new sfException("error file");
    }
  }
  private static $count_offre = array();
  private static $cache_member = array();
  
  public static function importOffres($file)
  {
    if(file_exists($file))
    {
      $xml = simplexml_load_file($file);
      
      
      $index=-1;
      foreach($xml as $xml_offre)
      {
        $index++;
        
        $id_adh = (string)$xml_offre->id_adh;
        if(isset(self::$cache_member[$id_adh]))
        {
          // membre inexistant on n'importe pas l'offre
          if(self::$cache_member[$id_adh] == null)
          {
            continue;
          }
          $member_id = self::$cache_member[$id_adh]->getId();
          self::$count_offre[$id_adh]++;
          // maxi 6 offres et demandes
          if(self::$count_offre[$id_adh] >6)
          {
            continue;
          }
        }
        else{
          $member = MemberTable::getByUsername($id_adh);
          self::$cache_member[$id_adh] = $member;
          self::$count_offre[$id_adh] = 1;
          if($member == null)
          {
            continue;
          }
          $member_id = $member->getId();
        }
        $offre = new LetsUserOffer();
        $name = (string)$xml_offre->intitule;
        $name = str_replace(array("\'",'\"'),array("'",'"'),$name);
        $offre->setName($name);
        $categ_id = intval((string)$xml_offre->classe2);
        if($categ_id >= 19)
        {
          $categ_id--;
        }
        $offre->setCategoryId($categ_id);
        $offre->setMemberId($member_id);
        $offre->setCreatedAt((string)$xml_offre->date);
        $offre->save();
      }
    }
  }
  public static function importDemandes($file)
  {
    if(file_exists($file))
    {
      $xml = simplexml_load_file($file);
      $cache_member = array();
      
      $index=-1;
      foreach($xml as $xml_offre)
      {
        $index++;
        
        $id_adh = (string)$xml_offre->id_adh;
        if(isset(self::$cache_member[$id_adh]))
        {
          // membre inexistant on n'importe pas l'offre
          if(self::$cache_member[$id_adh] == null)
          {
            continue;
          }
          $member_id = self::$cache_member[$id_adh]->getId();
          self::$count_offre[$id_adh]++;
          // maxi 6 offres et demandes
          if(self::$count_offre[$id_adh] >6)
          {
            continue;
          }
        }
        else{
          $member = MemberTable::getByUsername($id_adh);
          self::$cache_member[$id_adh] = $member;
          self::$count_offre[$id_adh] = 1;
          if($member == null)
          {
            continue;
          }
          $member_id = $member->getId();
        }
        $demande = new LetsUserAsk();
        $name = (string)$xml_offre->intitule;
        $name = str_replace(array("\'",'\"'),array("'",'"'),$name);
        $demande->setName($name);
        $categ_id = intval((string)$xml_offre->classe2);
        if($categ_id >= 19)
        {
          $categ_id--;
        }
        $demande->setCategoryId($categ_id);
        $demande->setMemberId($member_id);
        $demande->setCreatedAt((string)$xml_offre->date);
        $demande->setAskBeforeDelete(true);
        $dateExpire = '2011-09-30';
        $demande->setDateExpiration($dateExpire);
        $demande->setPeriodExpiration(LetsUserAskTable::DELAY_EXPIRATION_NEVER);
        $demande->save();
      }
    }
  }
}